define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.accountpay_id == '' || Config.accountpay_id == null){
        Config.accountpay_id = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionaccountpaystore/index/?accountpay_id=' + Config.accountpay_id,
                    add_url: 'feditionaccountpaystore/add/?accountpay_id=' + Config.accountpay_id,
                    edit_url: 'feditionaccountpaystore/edit',
                    del_url: 'feditionaccountpaystore/del',
                    multi_url: 'feditionaccountpaystore/multi',
                    import_url: 'feditionaccountpaystore/import',
                    table: 'fedition_accountpaystore',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'daystamp',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'accountpay_id', title: __('Accountpay_id'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccountpay/index' data-field='name'"},
                        {field: 'accountpay_name', title: __('Accountpay_id'), operate: false},
                        {field: 'accounturl_id', title: __('Accounturl_id'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('Accounturl_id'), operate: false},
                        {field: 'daystamp', title: __('Timedate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'refund_type', title: __('Refund_type'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'refund_type_name', title: __('Refund_type'), operate: false},
                        {field: 'refund_amount', title: __('Refund_amount'), operate:'BETWEEN'},
                        {field: 'payment_amount', title: __('Payment_amount'), operate:'BETWEEN'},
                        {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log('zamount',data.Statistics.zamount);
                $("#zamount").text(data.Statistics.zamount);
                $("#zadvertising").text(data.Statistics.zadvertising);
                $("#zsales").text(data.Statistics.zsales);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $("#c-accounturl_id").data("formatItem",function (item) {
                return "店铺域名:"+item.url+",店铺管理者:"+item.nickname;
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $("#c-accounturl_id").data("formatItem",function (item) {
                return "店铺域名:"+item.url+",店铺管理者:"+item.nickname;
            });
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        if (self.elem.hidden.is("#c-accounturl_id")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增店铺' data-window='top' href='feditionaccounturl/add'>新增店铺</a></p><p style='margin:5px;'>（新增店铺后需要取消下拉框，重新选择）</p>");
                        }
                        if (self.elem.hidden.is("#c-refund_type")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增金额类型' data-window='top' href='feditionmoneytype/add'>新增金额类型</a></p><p style='margin:5px;'>（新增金额类型后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});